﻿<%@ WebHandler Language="C#" Class="GpyProcess" %>

using System;
using System.Web;

public class GpyProcess : IHttpHandler {

    public void SaveFiles(HttpContext context, ExtBusiness.Common.AttachmentFileUpload ObjFile)
    {
        //创建服务器文件
        string iid = context.Request["IID"];
        string pathLocal = context.Server.MapPath("GpyUpload");
        pathLocal = pathLocal + "\\" + iid;

        System.IO.DirectoryInfo imagesfile = new System.IO.DirectoryInfo(pathLocal);
        if (!imagesfile.Exists)
        {
            imagesfile.Create();
        }
        pathLocal += "\\" + ObjFile.filename;


        System.IO.FileStream fs = new System.IO.FileStream(pathLocal, System.IO.FileMode.Create);
        //获得字节数组
        byte[] data = ObjFile.GetFilebuffer();
        //开始写入
        fs.Write(data, 0, data.Length);
        //清空缓冲区、关闭流
        fs.Flush();
        fs.Close();
    }
    
    public void ProcessRequest (HttpContext context) {
        try
        {
            System.IO.Stream stream = context.Request.InputStream;

            if (stream != null)
            {
                //存入数据库
                ExtBusiness.Common.AttachmentFileUpload ObjFile = new ExtBusiness.Common.AttachmentFileUpload();

                ObjFile.filename = context.Request["filename"];
                ObjFile.fileext = ".jpg";
                if (ObjFile.ReadFileStream(stream))
                {
                    SaveFiles(context, ObjFile);
                    //修正路径及文件名
                    string strNodeName = string.Format("({0}){1}", ObjFile.fileext, ObjFile.filename);
                    string strNodePath = strNodeName;

                    //插入数据库
                    if (!ObjFile.Update_St_Attachment(context.Request["IID"], strNodePath, strNodeName, context.Request["UserId"], context.Request["step"]))
                    {
                        //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                        context.Response.Write("0");
                    }
                }
                context.Response.Write("1");
            }
            else
            {
                context.Response.Write("0");
            }
        }
        catch (System.IO.IOException e)
        {
            context.Response.Write("0");
        }        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}